Imports System.Data.SqlClient
Imports CObject
Imports Objects.EmployeeInfo
Namespace Security
    ''' -----------------------------------------------------------------------------
    ''' Project	 : Objects
    ''' Class	 : Security.Validation
    ''' 
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Validates user security access to a page
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	 	3/2/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class Validation
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Validates the login of a user
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' 	 	2/13/2004	Created
        ''' </history>
        ''' 
        Public Enum ValidationResult
            Valid
            NotActivated
            NotRegistered
            InValid
            WrongPassword
        End Enum


        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Validates whether a given user has rights to see a given control
        ''' </summary>
        ''' <remarks>
        ''' If an item is not in the database it is not considered a secure object.
        ''' Therefore,  it will always be seen.
        ''' </remarks>
        ''' <history>
        ''' 	 	2/13/2004	Created
        ''' </history>
        ''' 
        Public Function ValidateForControl(ByVal psPagePath As String, ByVal psControlName As String, ByVal psGUI As String) As Boolean
            Dim loSecObject As SecurityObject
            Dim loConn As CCollection = New CCollection(New SecurityObject, "Name = '" & psControlName & "'")
            Dim loRoles As CDataTable
            Dim loControlRole As Role



            loSecObject = CType(loConn.DataObject, SecurityObject)
            If Not loSecObject Is Nothing Then
                If LCase(loSecObject.ParentObject.Name) = LCase(psPagePath) Then
                    loRoles = loSecObject.Roles
                    If Not loRoles Is Nothing Then
                        For Each loControlRole In loRoles
                            If loControlRole.ValidateGUI(psGUI) Then
                                Return True
                            End If
                        Next
                    End If
                End If
                Return False
            End If
            'Return true because it's not in the database.  If it's not in the database then it's not an item 
            'Subject to security
            Return True
        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Validates a user for a give page.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' 	 	2/13/2004	Created
        ''' </history>
        ''' 
        Public Function ValidatePage(ByVal psPagePath As String, ByVal psGUI As String) As Boolean
            Dim lbRet As Boolean = False
            Try
                Using loConn As ConnectionServer.Connection = New ConnectionServer.Connection(ConnectionServer.Connection.SourceDataBase.CMT)

                    lbRet = CBool(loConn.ExecuteScalar("EXEC SEC_UserHasRights '" & psPagePath & "','" & psGUI & "'"))
                End Using

            Catch ex As Exception
            
            End Try
            Return lbRet
        End Function

    End Class
End Namespace